﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 配置信息管理拓展类 ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using Wedn.Net.Common;
using Wedn.Net.Model;

namespace Wedn.Net.BLL
{
    /// <summary>
    /// 配置信息管理拓展类
    /// </summary>
    public partial class OptionManager
    {
        /// <summary>
        /// 获取设置信息字典
        /// </summary>
        /// <returns>设置信息字典</returns>
        public IDictionary<string, string> GetSetting()
        {
            List<Option> list = Select(o => true).ToList();
            IDictionary<string, string> dict = new Dictionary<string, string>();
            if (list.Count != 0)
            {
                foreach (var item in list)
                {
                    dict.Add(item.Name, item.Value);
                }
            }
            return dict;
        }

        public override Option Insert(Option entity)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Insert(entity);
        }
        public override Option Insert(Option entity, bool commit)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Insert(entity, commit);
        }
        public override IList<Option> Insert(params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Insert(entitys);
        }
        public override IList<Option> Insert(bool commit, params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Insert(commit, entitys);
        }

        /// <summary>
        /// 根据选项名称更新对应的选项值, 返回执行结果受影响行数
        /// </summary>
        /// <param name="name">要修改的选项名称</param>
        /// <param name="value">要更新的值</param>
        /// <returns>执行结果受影响行数</returns>
        public int UpdateValue(string name, string value)
        {
            Option entity = DAO.SelectSingle(o => o.Name == name);
            entity.Value = value;
            int row = Update(entity);
            //更新缓存
            Setting.Set(name, value);
            return row;
        }
        public override int Update(Option entity)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Update(entity);
        }
        public override int Update(Option entity, bool commit)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Update(entity, commit);
        }
        public override int Update(params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Update(entitys);
        }
        public override int Update(bool commit, params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Update(commit, entitys);
        }
        public override int Delete(Option entity)
        {
            Setting.Remove(entity.Name);
            return base.Delete(entity);
        }
        public override int Delete(Option entity, bool commit)
        {
            Setting.Remove(entity.Name);
            return base.Delete(entity, commit);
        }
        public override int Delete(params int[] ids)
        {
            int res = base.Delete(ids);
            Setting.Init(GetSetting());
            return res;
        }
        public override int Delete(bool commit, params int[] ids)
        {
            int res = base.Delete(commit, ids);
            Setting.Init(GetSetting());
            return res;
        }
    }
}
